<template>
    <div class="register-container">
        <!-- 背景装饰 -->
        <div class="medical-bg"></div>
        
        <!-- 主要内容区 -->
        <div class="register-content">
            <!-- 头部标题 -->
            <div class="header">
                <h1>欢迎加入桂宁健康</h1>
                <p class="subtitle">智能医疗 · 健康管理</p>
            </div>

            <!-- 注册表单 -->
            <div class="register-form">
                <p class="subtitle">填写以下信息，开始您的健康管理</p>

                <form @submit.prevent="handleSubmit">
                    <!-- 手机号输入 -->
                    <div class="input-group">
                        <i class="fas fa-phone"></i>
                        <input 
                            type="tel" 
                            placeholder="请输入手机号码"
                            v-model="users.userId"
                            maxlength="11"
                        >
                        <div class="field-tip">用于登录和接收健康提醒</div>
                    </div>

                    <!-- 姓名输入 -->
                    <div class="input-group">
                        <i class="fa fa-user-md"></i>
                        <input 
                            type="text" 
                            placeholder="请输入真实姓名"
                            v-model="users.realName"
                        >
                        <div class="field-tip">用于您的健康档案</div>
                    </div>

                    <!-- 生日选择 -->
                    <div class="input-group">
                        <i class="fa fa-calendar"></i>
                        <input 
                            type="date" 
                            v-model="users.birthday"
                            min="1950-01-01" 
                            max="2025-12-31" 
                        >
                        <div class="field-tip">您的出生日期</div>
                    </div>

                    <!-- 性别选择 -->
                    <div class="gender-group">
                        <div class="gender-label">性别</div>
                        <div class="gender-options">
                            <label class="gender-option">
                                <input 
                                    type="radio" 
                                    name="gender" 
                                    value="male" 
                                    v-model="users.sex"
                                >
                                <span class="gender-icon">
                                    <i class="fa fa-mars"></i>
                                </span>
                                <span>男</span>
                            </label>
                            <label class="gender-option">
                                <input 
                                    type="radio" 
                                    name="gender" 
                                    value="female" 
                                    v-model="users.sex"
                                >
                                <span class="gender-icon">
                                    <i class="fa fa-venus"></i>
                                </span>
                                <span>女</span>
                            </label>
                        </div>
                    </div>

                    <!-- 健康关注 (Dropdown) -->
                    <div class="input-group health-concern-group">
                        <i class="fa fa-notes-medical"></i>
                        <select v-model="selectedCondition" class="health-select">
                            <option disabled value="">请选择主要健康关注</option>
                            <option v-for="condition in conditionOptions" :key="condition" :value="condition">
                                {{ condition }}
                            </option>
                        </select>
                        <div class="field-tip">选择您最关注的健康问题</div>
                    </div>

                    <!-- 密码设置 -->
                    <div class="input-group">
                        <i class="fa fa-lock"></i>
                        <input 
                            :type="showPassword ? 'text' : 'password'"
                            placeholder="请设置6-12位密码"
                            v-model="users.password"
                        >
                        <i 
                            :class="['fa', showPassword ? 'fa-eye-slash' : 'fa-eye']"
                            @click="togglePassword"
                        ></i>
                        <div class="field-tip">请使用数字和字母的组合</div>
                    </div>

                    <!-- 确认密码 -->
                    <div class="input-group">
                        <i class="fa fa-lock"></i>
                        <input 
                            type="password"
                            placeholder="请再次输入密码"
                            v-model="confirmPassword"
                        >
                        <div class="field-tip">确保两次输入的密码一致</div>
                    </div>

                    <!-- 注册按钮 -->
                    <button type="submit" class="login-btn">
                        <i class="fa fa-check-circle"></i>
                        完成注册
                    </button>

                    <!-- 用户协议 -->
                    <div class="terms">
                        注册即表示您同意
                        <a href="#">用户协议</a>
                        和
                        <a href="#">隐私政策</a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</template>

<script>
import { reactive, ref } from "vue";
import axios from 'axios';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import 'element-plus/es/components/message/style/css';

export default {
    setup() {
        const router = useRouter();
        const showPassword = ref(false);
        const selectedCondition = ref(''); // For dropdown
        const confirmPassword = ref(''); // For confirm password field
        
        const conditionOptions = [
            "糖尿病",
            "高血压",
            "冠心病",
            "慢阻肺",
            "关节炎",
            "肾病",
            "哮喘",
            "痛风",
            "高血脂",
            "脑卒中",
            "脂肪肝",
            "骨质疏松"
        ];
        
        const users = reactive({
            userId: "",
            password: "",
            realName: "",
            birthday: "",
            sex: "",
        });
        
        const togglePassword = () => {
            showPassword.value = !showPassword.value;
        };
        
        const showAssist = () => {
            window.location.href = 'tel:10086';
        };
        
        const handleSubmit = async () => {
            // 基本验证
            if (users.password !== confirmPassword.value) {
                ElMessage({
                    message: '两次输入的密码不一致！',
                    type: 'warning',
                    duration: 3000
                });
                return;
            }
            
            if (users.password.length < 6 || users.password.length > 12) {
                ElMessage({
                    message: '密码长度必须在6-12位之间！',
                    type: 'warning',
                    duration: 3000
                });
                return;
            }

            if (!users.userId || !users.realName || !users.birthday || !users.sex || !selectedCondition.value) {
                ElMessage({
                    message: '请填写所有必填字段！',
                    type: 'warning',
                    duration: 3000
                });
                return;
            }

            try {
                const response = await axios.post('/api/user/register', {
                    username: users.userId,
                    password: users.password,
                    birthday: users.birthday,
                    sex: users.sex === 'male' ? 1 : 0,
                    name: users.realName,
                    symptom: selectedCondition.value
                });

                if (response.data.code === 200) {
                    ElMessage({
                        message: '注册成功！',
                        type: 'success',
                        duration: 2000
                    });
                    // 注册成功后跳转到登录页
                    router.push('/');
                } else {
                    ElMessage({
                        message: response.data.message || '注册失败',
                        type: 'error',
                        duration: 3000
                    });
                }
            } catch (error) {
                console.error('注册失败:', error);
                ElMessage({
                    message: '注册失败，请稍后重试',
                    type: 'error',
                    duration: 3000
                });
            }
        };
        
        return {
            users,
            showPassword,
            selectedCondition,
            confirmPassword,
            conditionOptions,
            togglePassword,
            showAssist,
            handleSubmit
        };
    }
}
</script>

<style scoped>
.register-container {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #f8f9fa;
    position: relative;
    overflow: hidden;
}

.medical-bg {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: 
        linear-gradient(135deg, rgba(76, 175, 80, 0.03) 0%, rgba(33, 150, 243, 0.03) 100%);
    z-index: 0;
}

.medical-bg::before,
.medical-bg::after {
    content: '';
    position: absolute;
    width: 300px;
    height: 300px;
    border-radius: 50%;
    background: rgba(33, 150, 243, 0.02);
}

.medical-bg::before {
    top: -100px;
    right: -100px;
}

.medical-bg::after {
    bottom: -100px;
    left: -100px;
}

.register-content {
    width: 100%;
    max-width: 420px;
    padding: 20px;
    position: relative;
    z-index: 1;
}

.header {
    text-align: center;
    margin-bottom: 20px;
}

h1 {
    color: #333;
    font-size: 24px;
    font-weight: 600;
    margin-bottom: 8px;
    line-height: 1.3;
}

.subtitle {
    color: #666;
    font-size: 14px;
}

.register-form {
    background: white;
    border-radius: 16px;
    padding: 30px;
    box-shadow: 0 8px 30px rgba(0, 0, 0, 0.05);
}

.input-group {
    display: flex;
    align-items: center;
    gap: 8px;
    position: relative;
    margin-bottom: 30px;
    width: 100%;
    max-width: 300px;
    margin-left: auto;
    margin-right: auto;
    padding-bottom: 18px;
}

.input-group i:first-child {
    color: #adb5bd;
    font-size: 17px;
    flex-shrink: 0;
}

/* Common styles for input and select */
.input-group input,
.input-group select.health-select {
    width: 100%;
    padding: 10px 15px;
    border: 1px solid #dee2e6;
    border-radius: 10px;
    font-size: 14px;
    transition: all 0.3s;
    height: 40px;
    flex-grow: 1;
    background-color: white;
    color: #333;
}

/* Styles specific to select dropdown */
.input-group select.health-select {
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
    background-image: url('data:image/svg+xml;charset=US-ASCII,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%22292.4%22%20height%3D%22292.4%22%3E%3Cpath%20fill%3D%22%23666666%22%20d%3D%22M287%2069.4a17.6%2017.6%200%200%200-13-5.4H18.4c-5%200-9.3%201.8-12.9%205.4A17.6%2017.6%200%200%200%200%2082.2c0%205%201.8%209.3%205.4%2012.9l128%20127.9c3.6%203.6%207.8%205.4%2012.8%205.4s9.2-1.8%2012.8-5.4L287%2095c3.5-3.5%205.4-7.8%205.4-12.8%200-5-1.9-9.2-5.5-12.8z%22%2F%3E%3C%2Fsvg%3E');
    background-repeat: no-repeat;
    background-position: right 15px center;
    background-size: .65em auto;
    padding-right: 35px;
}

.input-group select.health-select:invalid {
    color: #adb5bd;
}

/* Right padding for password/text inputs with eye icon */
.input-group input[type="password"],
.input-group input[type="text"] {
    padding-right: 35px;
}

/* Remove specific right padding from date/tel inputs */
.input-group input[type="date"],
.input-group input[type="tel"] {
    padding-right: 15px;
}

.input-group input:focus,
.input-group select.health-select:focus {
    border-color: #2196F3;
    box-shadow: 0 0 0 3px rgba(33, 150, 243, 0.1);
    outline: none;
}

.field-tip {
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    font-size: 12px;
    color: #95a5a6;
    padding-left: calc(17px + 8px);
    box-sizing: border-box;
    line-height: 1.4;
}

.gender-group {
    max-width: 300px;
    margin: 0 auto 25px;
}

.gender-label {
    font-size: 14px;
    color: #333;
    margin-bottom: 10px;
    padding-left: 12px;
}

.gender-options {
    display: flex;
    gap: 10px;
}

.gender-option {
    flex: 1;
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 12px;
    border: 1px solid #dee2e6;
    border-radius: 10px;
    cursor: pointer;
    transition: all 0.3s;
}

.gender-option:hover {
    border-color: #2196F3;
}

.gender-option input {
    display: none;
}

.gender-option input:checked + .gender-icon {
    color: #2196F3;
}

.gender-option input:checked + .gender-icon + span {
    color: #2196F3;
    font-weight: 500;
}

.gender-icon {
    color: #adb5bd;
    font-size: 16px;
}

.login-btn {
    width: 300px;
    padding: 12px;
    background: linear-gradient(135deg, #4CAF50, #2196F3);
    color: white;
    border: none;
    border-radius: 12px;
    font-size: 16px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    height: 44px;
    margin: 35px auto 15px;
}

.login-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 15px rgba(33, 150, 243, 0.2);
}

.terms {
    text-align: center;
    font-size: 12px;
    color: #666;
}

.terms a {
    color: #2196F3;
    text-decoration: none;
}

.terms a:hover {
    color: #1976D2;
}

@media (max-width: 480px) {
    .register-content {
        padding: 15px;
    }

    .register-form {
        padding: 20px;
    }

    .input-group {
        max-width: none;
        width: 95%;
    }

    .input-group input,
    .input-group select.health-select {
        font-size: 13px;
        padding: 10px 15px;
    }
    .input-group input[type="password"],
    .input-group input[type="text"] {
        padding-right: 35px;
    }
    .input-group select.health-select {
        padding-right: 35px;
    }

    .gender-group {
        max-width: none;
        width: 95%;
    }

    .login-btn {
        font-size: 15px;
        padding: 10px;
        width: 95%;
        max-width: 300px;
    }

    .input-group input[type="date"],
    .input-group input[type="tel"] {
        padding-right: 15px;
    }
}

.narrow-input {
    width: 80%;
    padding: 10px 20px;
    margin: 0 auto;
}
</style>